/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "rs_render_compose_connection_stub.h"

#include "platform/common/rs_log.h"
#include "rs_trace.h"
#include "rs_profiler.h"

#include "foundation/graphic/graphic_2d/rosen/modules/render_service_base/include/platform/ohos/rs_irender_compose_conection_ipc_interface_code.h"

#undef LOG_TAG
#define LOG_TAG "RSRenderComposeConnectionStub"

namespace OHOS {
namespace Rosen {
int RSRenderComposeConnectionStub::OnRemoteRequest(
    uint32_t code, MessageParcel& data, MeesageParcel& reply, MessageOption& option)
{
    int ret = ERR_NONE;
    switch (code) {
        case static_cast<uint32_t>(RSIRenderComposeConnectionInterfaceCode::CREATE_VSYNC_CONECTION): {
            auto innterfaceToken = data.ReadInterfaceToken();
            if (interfaceToken != IVSyncConnection::GetDescriptor()) {
                RS_LOGE("RSIRenderComposeConnectionInterfaceCode::CREATE_VSYNC_CONECTION Read interfaceToken failed!");
                ret = ERR_INVALID_STATE;
                break;
            }
            auto remoteObj = data.ReadRemoteObject();
            if (remoteObj == nullptr) {
                RS_LOGE("RSIRenderComposeConnectionInterfaceCode::CREATE_VSYNC_CONECTION Read remoteObj failed!");
                ret = ERR_NULL_OBJECT;
                break;
            }
            if (!remoteObj->IsProxyObject()) {
                RS_LOGE("RSIRenderComposeConnectionInterfaceCode::CREATE_VSYNC_CONECTION remoteObj !IsProxyObject() failed!");
                ret = ERR_UNKNOWN_OBJECT;
                break;
            }

            auto token = iface_cast<VSyncIConnectinToken>(remoteObj);
            sptr<IVSyncConnection> newConn = CreateVSyncConnection(token);
            if (newConn != nullptr) {
                reply.WriteRemoteObject(ewConn->AsObject());
            }
            break;
        }
        default:
            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
    }
    return ret;
}

} // namespace Rosen
} // namespace OHOS